package controller

import (
	"api/app/auth/models"
	"api/app/auth/service"
	"api/config/code"
	"api/libs/errs"
	"api/public/access"
	"gitee.com/dreamwood/ez"
	"gitee.com/dreamwood/ez/server"
)

func (this *PageController) MyPageAction(operation *server.Operation) {
	op := this.TakeOver(operation)
	token := op.GetAuthToken()
	if token == "" {
		op.ReturnError(code.ErrorAuth, "请登录", "")
		return
	}
	roles := access.GetUserRoleIdFromToken(token)
	isSa := false

	for _, role := range roles {
		if role == 1 {
			isSa = true
		}
	}
	crud := models.NewPageCrud()
	cond := ez.M{}
	if isSa {
		cond["isDelete"] = false
	} else {
		cond["roles.id__in"] = roles
	}
	pages, e := crud.FindBy(cond, []string{"l"}, 0, 0)

	if errs.Try(e) {
		op.ReturnError(code.ErrorMysql, "数据获取失败", "")
	}

	tree := service.CreatePageTree(0, pages)
	op.ReturnSuccess("OK", ez.M{
		"pages": tree,
		"roles": roles,
	})
}

func (this *ModelController) MyModelAction(operation *server.Operation) {
	op := this.TakeOver(operation)
	token := op.GetAuthToken()
	if token == "" {
		op.ReturnError(code.ErrorAuth, "请登录", "")
		return
	}
	roles := access.GetUserRoleIdFromToken(token)
	isSa := false

	for _, role := range roles {
		if role == 1 {
			isSa = true
		}
	}
	crud := models.NewModelCrud()
	cond := ez.M{}
	if isSa {
		cond["isDelete"] = false
	} else {
		cond["roles.id__in"] = roles
	}
	mds, e := crud.FindBy(cond, []string{"l"}, 0, 0)

	if errs.Try(e) {
		op.ReturnError(code.ErrorMysql, "数据获取失败", "")
	}

	tree := service.CreateModelTree(0, mds)
	op.ReturnSuccess("OK", ez.M{
		"models": tree,
		"roles":  roles,
	})
}
